
<template>
  <el-dialog v-dialogDrag :visible.sync="currentDialogVisible" :top="common.getDialogTop()" title="导入模板" class="dialog-container" width="750px" append-to-body>
    <el-form ref="form" :model="formData" label-width="120px">
      <el-form-item label="选择文件：">
        <el-upload ref="upload" class="upload-demo" :auto-upload="false" :action="designer.server + '/import'" :on-change="handleChange" :file-list="fileList" :headers="headers" :on-success="onSuccess">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button type="small" class="margin-left-10" @click="submitUpload">上传模板</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jxlxs文件</div>
        </el-upload>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="currentDialogVisible = false">关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    designer: {
      type: Object,
      default: () => {
        return {};
      }
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formData: {
        fileName: null,
        prefix: null
      },
      fileList: []
    };
  },
  computed: {
    // 显示窗口
    currentDialogVisible: {
      get: function () {
        return this.visible;
      },
      set: function (val) {
        this.$emit("update:visible", val);
      }
    },
    // headers
    headers: {
      get: function () {
        const userInfo = this.common.getUserInfo();
        return {
          accessToken: userInfo.accessToken
        };
      }
    }
  },
  mounted () {},
  methods: {
    submitUpload () {
      this.$refs.upload.submit();
    },
    handleChange (file, fileList) {
      this.fileList = fileList.slice(-3);
    },
    onSuccess (response, file, fileList) {
      const res = response.data;
      if (res.result) {
        this.designer.loadFile("classpath:static/template/template.ureport.xml");
        this.currentDialogVisible = false;
      } else {
        this.$message.error(res.message);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.dialog-container {
  padding: 0;
  .tip-item {
    line-height: 28px;
    color: darkgrey;
  }
}
</style>
